#include "stdafx.h"

void Application::check_mouse_hover()
{
    ray.setOrigin(n_cam->getPosition());
    ray.setDirection(n_cam->getOrientation() * Vector3(0,0,-1));
    rsq->setRay(ray);
    camera->getCameraToViewportRay(0.5f,0.5f,&ray);
    rsq->execute();
    //    typedef vector<RaySceneQueryResultEntry>::type RaySceneQueryResult;
    //for(RaySceneQueryResult::iterator
    //    a = rsq->getLastResults().begin(); a != rsq->getLastResults().end(); ++a)
    previous_rayed = current_rayed;
    if(! rsq->getLastResults().empty()) // we need to raycast each frame in order to know
    {        
        current_rayed = static_cast<Entity*>(rsq->getLastResults().begin()->movable);
        if(current_rayed != previous_rayed && previous_rayed != 0) // reset previous rayed
        {
            previous_rayed->setMaterialName("hover/idle");
            current_rayed->setMaterialName("hover/hover");
            //find_near_objects( // call there
            //    current_rayed->getParentSceneNode()->getPosition(), 5);
        }
    }
    else
        current_rayed->setMaterialName("hover/idle");
    
    //current_rayed->getMat
    //sphq->
}

void Application :: find_near_objects(Ogre::Vector3 pos, float radius)
{   // not callable yet
    sph.setCenter(pos);
    sph.setRadius(radius);
    //SceneQueryResult& b = sphq->execute();

    for(auto a = sphq->getLastResults().movables.begin(); a != sphq->getLastResults().movables.end(); ++a)
    {
        static_cast<Entity*>(*a)->setMaterialName("hover/hover");
    }
}

void Application :: init_other()
{
    sph = Sphere();
    sph.setRadius(8);
    rsq = new DefaultRaySceneQuery(scmgr);
    sphq = scmgr->createSphereQuery(sph, qf_movable);
    rsq->setQueryMask(qf_movable);
    current_rayed = scmgr->getEntity("bonome");

    sphq->setSphere(sph);
}

